#include "http.h"

Http::Http()
{

}

bool Http::post_sync(QString url, QMap<QString, QString> header, QByteArray &requestData, QByteArray &replyData)
{
    QNetworkAccessManager manager;  //发送请求的动作

    QNetworkRequest request;    //请求的内容(包含url和头)
    request.setUrl(url);
    QMapIterator<QString, QString> it(header);
    while(it.hasNext())
    {
        it.next();
        request.setRawHeader(it.key().toLatin1(), it.value().toLatin1());
    }

    QNetworkReply *replay = manager.post(request, requestData);
    QEventLoop l;
    connect(replay, &QNetworkReply::finished, &l, &QEventLoop::quit);
    l.exec();   //死循环

    if(replay != nullptr && replay->error() == QNetworkReply::NoError)
    {
        replyData = replay->readAll();
        return true;
    }
    else
    {
        return false;
    }
}
